package com.touch.sound.core;
/*
 * Copyright (c) 2009, TOD Productions All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
 *	- Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
 *	- Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
 *	-Neither the name of the organization nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. 
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 */
import java.net.*;
public abstract class AbstractSoundPlayer extends Thread
{
	private boolean ended = false;
	private boolean isAlive = true;
	private boolean isPaused = false;
	private boolean isLooped = false;
	private boolean isStarted = false;
	private String src;
	
	public AbstractSoundPlayer()
	{
		this.setPriority(Thread.MIN_PRIORITY);
	}
	public void load(URL src) throws MalformedURLException
	{		
		this.src = src.toString();		
		importSource(new URL(this.src));
	}
	public boolean isEnded()
	{
		return this.ended;
	}
	public abstract void importSource(URL src);	
	protected abstract boolean codec();	
	public void play()
	{
		this.isAlive = true;
		this.isPaused = false;
		this.isLooped = false;		
		if(this.isStarted == false)
		{
			this.isStarted = true;
			this.start();
		}
	}
	public void loop()
	{
		this.isAlive = true;
		this.isPaused = false;
		this.isLooped = true;
		if(this.isStarted == false)
		{
			this.isStarted = true;
			this.start();
		}
	}
	public void pauseAudio()
	{
		this.isPaused = true;
	}
	public void resumeAudio()
	{
		this.isPaused = false;
	}
	public void destroy()
	{
		this.isAlive = false;
	}		
	public void run()
	{
		while(this.isAlive)
		{
			try
			{				
				if(!this.isPaused)
				{					
					while(codec())
					{
						if(this.isAlive == false)
							return;
						sleep(10);	
					}					
					if(this.isLooped)
					{											
						load(new URL(this.src));
					}
					else					
						this.ended = true;											
				}
				sleep(100);
			}
			catch(Exception e)
			{
				e.printStackTrace();
			}
		}
		System.out.println("ending thread...");
	}
	public void sleep(int ms) throws InterruptedException
	{
		Thread.sleep(ms);
	}
	public String toString()
	{
		return this.src;
	}
}

